package com.goodving.guli.view;

import android.app.ListFragment;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.goodving.guli.activity.SettingContainerActivity;
import com.goodving.guli.bt.profile.scale.GuliLog;

/**
 * Created by john on 2015/11/24.
 */
public class DeviceListFragment extends ListFragment {
    public static final String f8924a = "DeviceListFragment";
    public static final int f8925b = 1;
    public static final int f8926c = 2;
    private static final int f8927d = 0;
    private static final int f8928e = 1;
    private int f8929f;
    private GuliDevicesListAdapter devicesListAdapter;
//    private C1630e f8931h; TBD this is for shoe
    private Context f8932i;

    public DeviceListFragment() {
        this.f8929f = f8928e;
    }

    // all these are for shoes
//    private void m13755a() {
//        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
//        if (defaultAdapter != null) {
//            if (defaultAdapter.isEnabled()) {
//                m13756b();
//            } else {
//                startActivityForResult(new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE"), f8927d);
//            }
//        }
//    }

    private void m13756b() {
//        startActivity(ShoesBindActivity.m13299a(this.f8932i, this.f8931h));
    }

    public void m13758a(int i) {
        this.f8929f = i;
    }

    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        getListView().setDivider(null);
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        if (i != 0) {
            if (i == f8928e) {
                switch (i2) {
                    case -1 /*-1*/:
                        m13756b();
                        break;
                    case f8927d /*0*/:
                        getActivity().finish();
                        break;
                    default:
                        break;
                }
            }
        }
        switch (i2) {
            case -1 /*-1*/:
                m13756b();
                break;
            case f8927d /*0*/:
                try {
                    startActivityForResult(new Intent("cn.com.smartdevices.bracelet.intent.action.ENABLE_BLUETOOTH"), f8928e);
                    break;
                } catch (ActivityNotFoundException e) {
                    GuliLog.tracePrintInfo(f8924a, "EnableBluetoothActivity not found, may be the module is running alone");
                    break;
                }
        }
        super.onActivityResult(i, i2, intent);
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.f8932i = getActivity().getApplicationContext();
        this.devicesListAdapter = new GuliDevicesListAdapter(this.f8932i);
        setListAdapter(this.devicesListAdapter);
    }

    public void onListItemClick(ListView listView, View view, int i, long j) {
        super.onListItemClick(listView, view, i, j);
        GuliDevicesListAdapter.GuliHardwareDeviceItem c1822z = (GuliDevicesListAdapter.GuliHardwareDeviceItem) getListView().getItemAtPosition(i);
        if (c1822z.guliHardwareDevice == GuliHardwareDevice.RING || c1822z.guliHardwareDevice == GuliHardwareDevice.SCALE) {
            Intent intent = new Intent(this.f8932i, SettingContainerActivity.class);
            intent.putExtra(SettingContainerActivity.f9257a, c1822z.guliHardwareDevice.ordinal());
            startActivity(intent);
        }
    }

    public void onResume() {
        super.onResume();
        this.devicesListAdapter.refreshDeviceList();
        this.devicesListAdapter.notifyDataSetChanged();
    }
}
